package cn.ffcs.security.web;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.security.model.Resource;
import cn.ffcs.security.model.Role;
import cn.ffcs.security.model.RoleRelaResource;
import cn.ffcs.security.service.ResourceService;
import cn.ffcs.security.service.RoleService;

/**
 * 
 * 角色与资源关联关系 action层，进行业务逻辑操作<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class RoleRelaResourceAction<R extends RoleRelaResource> extends
        BaseAction<R>
{

    /** 网络标识  */
    private static final long serialVersionUID = -5336084293308611704L;

    /** 业务POJO **/
    private R roleRelaResource;

    /** 业务列表数据 **/
    private List<R> roleRelaResources;
    
    /** 资源列表数据 **/
    private List<Resource> resources;
    
    /** 角色服务 **/
    private RoleService roleService;
    
    /** 资源服务 **/
    private ResourceService resourceService;

    /** 已选择资源 **/
    private String selectedResources;
    
    /**
     * 
     * 初始化业务数据
     * @return
     */
    public String init(){
        
        //获取所有资源数据
        resources=resourceService.findAll();
        //获取用户角色数据
        HashMap<String, Object> hmCond=new HashMap<String, Object>();
        hmCond.put("role.id", roleRelaResource.getRole().getId());
        setConds(hmCond);
        roleRelaResources=findBy();
        //初始化已选择资源数据
        String selRsc="";
        for (RoleRelaResource r:roleRelaResources){
            if (StringUtils.isEmpty(selRsc)){
                selRsc=r.getResource().getId().toString();
            }else{
                selRsc+=","+r.getResource().getId().toString();
            }
        }
        selectedResources=selRsc;
        //获取实体
        Role role=(Role)roleService.findById(roleRelaResource.getRole().getId());
        roleRelaResource.setRole(role);
        
        return SUCCESS;  
    }
    
    /**
     * 
     * 保存角色数据
     * @return
     */
    public String saveRoleRelaResource(){
        //先删除对应资源数据
        baseService.deleteBy(roleRelaResource, "role", roleRelaResource.getRole().getId().toString());
        
        //数据保存 操作; 
        if (StringUtils.isNotEmpty(selectedResources)){
            String[] roles=selectedResources.split(",");
            for (String roleId:roles){
            	
                roleRelaResource.setId(null);
                Resource resource=new Resource();
                resource.setId(Integer.parseInt(roleId));
                roleRelaResource.setResource(resource);
                
                
                //RoleRelaResource relaResource=new RoleRelaResource();
            	//relaResource.setRole(roleRelaResource.getRole());
            	//relaResource.setResource(resource);
            	
                baseService.saveOrUpdate(roleRelaResource);
                //baseService.merge(roleRelaResource);
            }
        }
        
        //初始化
        init();
        
        return SUCCESS;        
    }
    
    /**
     * 
     * 初始化页面数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String authResource()
    {
        //获取所有资源数据
        resources=resourceService.findAll();
        //获取用户资源数据
        roleRelaResources=findBy();
        //获取实体
        Role role=(Role)roleService.findById(roleRelaResource.getRole().getId());
        roleRelaResource.setRole(role);
        //初始化
        init();
        
        return SUCCESS;
    }

    /**
     * 
     * 获取pojo
     * @return
     */
    public R getRoleRelaResource()
    {
        return roleRelaResource;
    }
    
    /**
     * 
     * 设置pojo
     * @return
     */
    public void setRoleRelaResource(R roleRelaResource)
    {
        this.roleRelaResource = roleRelaResource;
    }
    
    /**
     * 
     * 获取角色资源列表
     * @return
     */
    public List<R> getRoleRelaResources()
    {
        return roleRelaResources;
    }
    
    /**
     * 
     * 设置角色资源列表
     * @return
     */
    public void setRoleRelaResources(List<R> roleRelaResources)
    {
        this.roleRelaResources = roleRelaResources;
    }

    /**
     * 获取资源列表数据
     * @return the resoures
     */
    public List<Resource> getResources()
    {
        return resources;
    }

    /**
     * 设置资源列表数据
     * @param resoures the resoures to set
     */
    public void setResources(List<Resource> resources)
    {
        this.resources = resources;
    }

    /**
     * 获取角色服务
     * @return the roleService
     */
    public RoleService getRoleService()
    {
        return roleService;
    }

    /**
     * 设置角色服务
     * @param roleService the roleService to set
     */
    public void setRoleService(RoleService roleService)
    {
        this.roleService = roleService;
    }

    /**
     * 获取资源服务
     * @return the resourceService
     */
    public ResourceService getResourceService()
    {
        return resourceService;
    }

    /**
     * 设置资源服务
     * @param resourceService the resourceService to set
     */
    public void setResourceService(ResourceService resourceService)
    {
        this.resourceService = resourceService;
    }

    /**
     * 获取已选择资源
     * @return the selectedResources
     */
    public String getSelectedResources()
    {
        return selectedResources;
    }

    /**
     * 设置已选择资源
     * @param selectedResources the selectedResources to set
     */
    public void setSelectedResources(String selectedResources)
    {
        this.selectedResources = selectedResources;
    }

}
